package com.onpositive.data.serializer.readers;

import java.util.BitSet;


public class ComparingAggregator  {

	public static final int MIN_MODE = 0;
	public static final int MAX_MODE = 1;

	protected int mode = ComparingAggregator.MIN_MODE;

	public ComparingAggregator() {
		super();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object getAggregatedValue(Object[] values,BitSet ss) {
		if ((values.length > 0)) {
			Comparable<?> res = (Comparable<?>) values[0];
			int a=0;
			for (Object i : values) {
				if (i == null) {
					a++;
					continue;
				}
				if (ss.get(a)){
					a++;
					continue;
				}
				if (res == null) {
					res = (Comparable<?>) i;
				}
				if (((this.mode == ComparingAggregator.MIN_MODE) && (((Comparable) i)
						.compareTo(res) < 0))
						|| ((this.mode == ComparingAggregator.MAX_MODE) && (((Comparable) i)
								.compareTo(res) > 0))) {
					res = (Comparable<?>) i;
				}
				a++;
			}
			return res;
		}
		return null;
	}

	public int getMode() {
		return this.mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}
}
